package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BusConst;
import cn.stylefeng.guns.sys.modular.system.entity.Enum.WxMsgPushEnum;
import cn.stylefeng.guns.sys.modular.system.entity.WxmsgPush;
import cn.stylefeng.guns.sys.modular.system.mapper.WxmsgPushMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.WxmsgPushParam;
import cn.stylefeng.guns.sys.modular.system.model.result.WxInfoResult;
import cn.stylefeng.guns.sys.modular.system.model.result.WxmsgPushResult;
import cn.stylefeng.guns.sys.modular.system.service.WeChatSendTempMsgService;
import  cn.stylefeng.guns.sys.modular.system.service.WxmsgPushService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 微信消息推送 服务实现类
 * </p>
 *
 * @author gong
 * @since 2021-03-09
 */
@Service
public class WxmsgPushServiceImpl extends ServiceImpl<WxmsgPushMapper, WxmsgPush> implements WxmsgPushService {

    private static final Logger log= LoggerFactory.getLogger(WxmsgPushServiceImpl.class);

    @Override
    public void add(WxmsgPushParam param){
        WxmsgPush entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WxmsgPushParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WxmsgPushParam param){
        WxmsgPush oldEntity = getOldEntity(param);
        WxmsgPush newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WxmsgPushResult findBySpec(WxmsgPushParam param){
        return null;
    }

    @Override
    public List<WxmsgPushResult> findListBySpec(WxmsgPushParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(WxmsgPushParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(WxmsgPushParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private WxmsgPush getOldEntity(WxmsgPushParam param) {
        return this.getById(getKey(param));
    }

    private WxmsgPush getEntity(WxmsgPushParam param) {
        WxmsgPush entity = new WxmsgPush();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public void addGcPushMsg(List<WxInfoResult> wxlist, Long businessId, Integer taskId, JSONObject msg){
        List<WxmsgPush> list = new ArrayList<>();
        for(WxInfoResult wx:wxlist){
            //判断是否存在
            QueryWrapper<WxmsgPush> qw = new QueryWrapper();
            qw.eq("openid",wx.getOpenid()).eq("business_id",businessId).eq("task_id",taskId);
            List<WxmsgPush> existList = this.baseMapper.selectList(qw);
            if(existList.size()>0){
                log.info("表里已存在该数据openid:{},proid:{},taskid:{}",wx.getOpenid(),businessId,taskId);
                continue;
            }
            WxmsgPush wxmsg = new WxmsgPush();
            wxmsg.setOpenid(wx.getOpenid());
            wxmsg.setName(wx.getName());
            wxmsg.setPhone(wx.getPhone());
            wxmsg.setStatus(WxMsgPushEnum.STATUS_TOPUSH.getCode());
            wxmsg.setTemplateId(WxMsgPushEnum.TEMPLATE_GC.getValue());
            wxmsg.setBusinessType(WxMsgPushEnum.BUSINESS_TYPE.getCode());
            wxmsg.setBusinessId(businessId);
            wxmsg.setTaskId(taskId);
            wxmsg.setMsg(msg.toString());
            wxmsg.setJumpUrl(BusConst.MSURL);
            list.add(wxmsg);
        }
        this.saveBatch(list);
    }


    @Override
    public List<WxmsgPush> listToPushData(WxmsgPushParam param){
        param.setNowTime(DateUtil.getNow());
        return this.baseMapper.listToPushData(param);
    }


}
